
//
//  CellForViewController4.m
//  ProjectA
//
//  Created by dllo on 16/1/30.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "CellForViewController4.h"
#import "AppMacro.h"
#import "ModelForViewController4.h"
#import <UIImageView+WebCache.h>
@interface CellForViewController4 ()
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *cover;
@property (nonatomic, strong) UILabel *label;
@end

@implementation CellForViewController4

- (void)dealloc {
    
    
    [_tableView release];
    [_cover release];
    [_label release];
    [self safeRemoveObserver];
    [super dealloc];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setup];
   
        
    }
    return self;
}
#define WIDTH self.bounds.size.width
#define HEIGHT self.bounds.size.height
- (void)setup {
    
    self.cover = [[UIImageView alloc] init];
    [self.contentView addSubview:self.cover];
    [_cover release];
    [self.contentView sendSubviewToBack:self.cover];
    
    //self.cover.contentMode = UIViewContentModeScaleAspectFill;
    self.clipsToBounds = YES;
    self.parallaxRatio = 1.5f;
    
    self.label = [UILabel new];
    [self.contentView addSubview:self.label];
    [_label release];
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    [self safeRemoveObserver];
    UIView *v = newSuperview;
    while (v) {
        if ([v isKindOfClass:[UITableView class]]) {
            self.tableView = (UITableView *)v;
            break;
        }
        v = v.superview;
    }
    [self safeAddObserver];
    
}
- (void)removeFromSuperview {
    [super removeFromSuperview];
    [self safeRemoveObserver];
}
- (void)safeAddObserver {
    if (self.tableView) {
        @try {
            [self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        }
        @catch (NSException *exception) {
            
        }
        
    }
}
- (void)safeRemoveObserver {
    if (self.tableView) {
        @try {
            [self.tableView removeObserver:self forKeyPath:@"contentOffset" context:nil];
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            self.tableView = nil;
        }
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.label.frame = CGRectMake(10, (self.frame.size.height / 2) - 15, self.frame.size.width - 20, 30);
    self.label.font = [UIFont systemFontOfSize:25];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.cover.layer.cornerRadius = 10;
    self.cover.layer.masksToBounds = YES;
    
    
   [UIView setColorWithLabelText:self.label nightColor:L_COLOR_WHITE dayColor:L_COLOR_WHITE];
    
    self.parallaxRatio = self.parallaxRatio;
    return;
}


- (void)passData:(ModelForViewController4 *)model {
    
    
    [self.cover sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"iconfont-zhanweitu"]];
    self.label.text = model.name;
    
}

- (void)setParallaxRatio:(CGFloat)parallaxRatio
{
    _parallaxRatio = MAX(parallaxRatio, 1.0f);
    _parallaxRatio = MIN(parallaxRatio, 2.0f);
    
    CGRect rect = self.bounds;
    rect.size.height = rect.size.height * parallaxRatio;
    self.cover.frame = rect;
    
    [self updateParallaxOffset];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ( [keyPath isEqualToString:@"contentOffset"] )
    {
        if ( ![self.tableView.visibleCells containsObject:self] || (self.parallaxRatio == 1.0f) )
        {
            return;
        }
        
        [self updateParallaxOffset];
    }
}

- (void)updateParallaxOffset
{
    CGFloat contentOffset = self.tableView.contentOffset.y;
    
    CGFloat cellOffset = self.frame.origin.y - contentOffset;
    
    CGFloat percent = (cellOffset + self.frame.size.height) / (self.tableView.frame.size.height + self.frame.size.height);
    
    CGFloat extraHeight = self.frame.size.height * (self.parallaxRatio - 1.0f);
    
    CGRect rect = self.cover.frame;
    rect.origin.y = -extraHeight * percent;
    self.cover.frame = rect;
}




- (void)awakeFromNib {
    // Initialization code
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
